package com.frxuexi.myroom.view

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.Divider
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.Leave
import com.frxuexi.myroom.intent.LeaveIntent
import com.frxuexi.myroom.viewmodel.LeaveViewModel
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 * 审批请假
 */
@Composable
fun ComView(navHostController: NavHostController,json:String,vm: LeaveViewModel = hiltViewModel()) {
    var uname by remember {
        mutableStateOf(MMKV.defaultMMKV().decodeString("name", "未登录").toString())
    }

    val entity = Gson().fromJson(json, Leave::class.java)
    var status by remember {
        mutableStateOf("通过")
    }
    var comReason by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("审批完成")
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })
    Column {
        BaseTitle(title = "请假审批", navHostController = navHostController)
        
        Column(
            Modifier
                .fillMaxSize()
                .padding(horizontal = 20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            Text(text = "申报人信息", color = Color.Blue)
            Row(modi) {
                AsyncImage(
                    model = "https://img0.baidu.com/it/u=797474174,3214837072&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1732294800&t=f472f47d1015da000b2194ee3b7a3c4a",
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
                Text(text = uname)
            }

            Spacer(modifier = Modifier.height(10.dp))
            Text(text = "请假详情", color = Color.Blue)
            Text(text = "请假方式: ${entity.type}")
            Text(text = "开始时间: ${entity.startTime}")
            Text(text = "结束时间: ${entity.endTime}")
            Text(text = "请假原因: ${entity.reason}")

            Spacer(modifier = Modifier.height(10.dp))
            Text(text = "审批意见", color = Color.Blue)
            Divider(
                /*Modifier
                    .fillMaxWidth()
                    .height(2.dp)*/)
            Row (verticalAlignment = Alignment.CenterVertically){
                Text(text = "意见内容：")
                RadioButton(selected = status=="通过", onClick = { status="通过" })
                Text(text = "通过")
                RadioButton(selected = status=="拒绝", onClick = { status="拒绝" })
                Text(text = "拒绝")
            }
            Row {
                Text(text = "意见原因：")
                OutlinedTextField(value = comReason, onValueChange = {comReason=it}, label = { Text(text = "审批原因")},
                    modifier = Modifier.fillMaxWidth())
            }


            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                entity.status=status
                entity.approveTime=SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
                vm.sendIntent(LeaveIntent.update(entity))
            }) {
                Text(text = "提交")
            }
        }
    }
}